
#include "StdAfx.h"
#include "AllUser_RoomExit_Default.h"
#include "board_define.h"
#include "User.h"
#include "Room.h"
#include "UserManager.h"
#include "LobbyService.h"
#include <LogService.h>

int AllUser_RoomExit_Default::exit( Room* room ){	
	int result = AllUser_RoomExit_Decorator::exit( room );
	if( result )
		return result;	
	
	UserManager* users = LobbyService::Instance()->getUserManager();
	RoomExit* pRoomExit = LobbyService::Instance()->getRoomExit();
	LobbyEnter* pLobbyEnter = LobbyService::Instance()->getLobbyEnter();		
	User* user = 0;	
	int observer_count = 0;
	size_t i = 0;
	
	for( i = 0 ; i < LobbyService::Instance()->getRoomUserMax() ; i++ ){
		user = room->getUserSlot( i );
		IS_NULL_ACTION( user, continue );
				
		result = pRoomExit->exit( user, 0, true );			
		if( result )
			LOG_ERR( LOG_SERVICE( "instance" )->Log("%s - %u", __FUNCTION__, __LINE__) );		

		result = pLobbyEnter->enter( 0, user );
		if( result )
			users->removeUser( user );
	}	

	observer_count = room->getObserverCount();
	for( i = 0 ; i < (size_t)observer_count ; i++ ){
		user = room->getUserObserverSlotIdx( i );
		IS_NULL_ACTION( user, continue );
				
		result = pRoomExit->exit( user, 0, true );			
		if( result )
			LOG_ERR( LOG_SERVICE( "instance" )->Log("%s - %u", __FUNCTION__, __LINE__) );		

		result = pLobbyEnter->enter( 0, user );
		if( result )
			users->removeUser( user );			
	}

	return DEFAULT_SUCCESS;
}
